GBML manual / Data Containers

Accessors

How to read or write one element of a container in place - plain [i] for array and list, [# x, y] for a grid - and why map, stack, queue and set never offer one at all.

What an accessor is

An accessor is the square-bracket syntax right after a container variable - board[3], board[# 1, 2] - that reaches exactly one element in place, without calling a named function. It is part of GBML's expression grammar, not a function call, and it works on either side of =: the same container[...] reads a value out and writes one in.

Only three of the seven containers offer one at all, and only one spelling exists for each: plain [i] for array<T>/list<T>, and [# x, y] for grid<T>. Nothing on this manual ever needs a different bracket shape for the same job - there is no separate "list accessor" and "array accessor," just the one plain form both share.

Plain [i] - array and list

array<T> and list<T> are indexed exactly the same way, because they are stored exactly the same way underneath - the only difference between the two is which named functions additionally exist (see Arrays for that choice). One number, zero-based, counting from the first slot:

scores[0] = 10;        // the first slot
scores[1] = 20;
var first = scores[0]; // 10

The slot has to exist. Slot numbers start at 0, so scores[-1] is always wrong, and so is a number past the end of an array whose size is written right there in your code (scores[4] on [10, 20, 30]). The editor stops you at that line with a message like "There is no slot 4 in scores: it only has 3 slots". When the number is only worked out while the game runs (scores[i]), the editor cannot know it, and a slot that is not there stops the game with an "Invalid index" message. A list<T> can grow and shrink, so the editor only catches a negative number there.

To keep a loop inside the places that exist, ask the container how many it has: array_length for an array, list_length for a list. for (var i = 0; i < array_length(scores); i++) never reaches a slot that is not there.

Two numbers - [# x, y] for a grid

grid<T> is two-dimensional, so its accessor takes two numbers instead of one - the column, then the row, always in that order, separated by a comma:

board[# 2, 0] = 5;           // column 2, row 0
var cell = board[# 2, 0];    // read it back - 5

Always write a space after the #. [#2, 0] - the # written tight against a digit, or a letter a-f - reads as the start of a #RRGGBB color literal instead, not the start of an accessor. See Grids for declaring a grid's width and height, which [# x, y] needs to turn two numbers into one real slot. To keep a loop inside the grid, grid_width is how many columns it has (x runs from 0 up to one less than that) and grid_height is how many rows (y does the same).

No accessor at all - stack, queue and set

stack<T>, queue<T> and set<T> do not offer [i], or any other bracket form - on purpose, not because nobody got round to it. Each one's entire appeal is that you can only ever reach one particular end, or ask one particular yes/no question, never the middle:

If a project genuinely needs to walk every element in order, that is what list<T> is for - Data containers covers the parallel-list pattern for exactly this case.

map<K,V>: no accessor either, despite the ?

map<K,V> looks like it should be the fourth exception - GBML's grammar really does recognize a [? key] accessor, and a map really is the one container this manual describes as "looked up by key." Try it anyway and the compiler refuses it, the same "cannot be indexed" refusal stack<T>/queue<T>/set<T> already get:

A map<int,string> cannot be indexed with `[...]`.

This is deliberate, not a gap the parser forgot to close. An earlier version of the language did give [? key] real meaning, built on the same two risky moves a raw lookup always has: read a missing key and it crashes, or write one and it inserts a brand new entry - which can overflow and crash a map that is already full, since an insert is the one map operation that actually needs a free slot. Both are worse failures than simply refusing the syntax outright. map_get/map_set replace both, safely. map_get(map, key) takes only the map and the key - there is no third "what to give back" value. For a key that was never set it hands back a plain zero-like value for the map's value type (0, 0.0, false, empty text, or nothing) instead of crashing; ask map_has first when you need to tell that apart from a real stored value. map_set reports false instead of overflowing a full map. Reach for those, plus map_has/map_length/map_clear, for everything a map is for - [? key] stays legal to type, and always refused the moment it is checked.

Getting the accessor wrong vs. having none at all

Two different messages cover two different mistakes, and it is worth telling them apart. Use the wrong bracket shape on a container that does have one - board[0] on a real grid<T>, say, instead of board[# x, y] - and the compiler names the fix:

This is a grid<int>, which uses `[# x, y]`, not `[i]`.

Use any bracket shape at all on a container with no accessor - stack<T>, queue<T>, set<T>, or map<K,V> - and it is always the plainer refusal instead, never the grid one, because there is no "closer" spelling to point you toward:

A stack<int> cannot be indexed with `[...]`.

Example

/// @type {list<int>}
/// @capacity 4
var scores = [10, 20, 30];
scores[1] = 99;

/// @type {array<int>}
/// @capacity 3
var lookup = [1, 2, 3];
var first = lookup[0];

/// @type {grid<int>}
/// @capacity 2x2
var board = [];
board[# 0, 0] = 7;
var corner = board[# 0, 0]; // 7

See also

Data containers covers all seven containers side by side, including which of them this page's accessors apply to. Arrays and Grids cover their own declaration and capacity rules in full.

See also: Data containersArraysGridsarray_lengthgrid_widthgrid_heightlist_addmap_getstack_push